home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / hardware / pas_src / joystick.pas < prev    next >
Pascal/Delphi Source File  |  1994-06-17  |  2KB  |  114 lines

  1. {
  2. --------------------------------------------------
  3. File:     JOYSTICK.PAS
  4. By:       Ronny Wester, ronny@rat.se
  5.  
  6. Code to read PC sticks.
  7. Written with kind assistance of Christian Wagner.
  8. --------------------------------------------------
  9. }
  10. unit Joystick;
  11.  
  12.  
  13. interface
  14.  
  15.  
  16. type
  17.  
  18.   JoyRec =
  19.     record
  20.       present : Boolean;
  21.       x, y : Word;
  22.       button1,
  23.       button2,
  24.       button3,
  25.       button4 : Boolean;
  26.     end;
  27.  
  28. var
  29.  
  30.   gSticks : array [1..2] of JoyRec;
  31.  
  32.  
  33. procedure PollSticks;
  34. procedure InitSticks;
  35.  
  36.  
  37. implementation
  38.  
  39.  
  40. const
  41.  
  42.   cStickPort = $0201;
  43.  
  44.  
  45. { PollSticks. Run to retrieve current values of sticks.
  46.   Only sticks with the present flag set will be polled.
  47.   Also see InitSticks below. }
  48.  
  49. procedure PollSticks;
  50. var b : Byte;
  51.     mask : Byte;
  52.     laps : Word;
  53. begin
  54.   mask := 0;
  55.   if gSticks[1].present then
  56.     mask := 3;
  57.   if gSticks[2].present then
  58.     mask := mask or 12;
  59.   FillChar( gSticks, SizeOf( gSticks), 0);
  60.   laps := 0;
  61.  
  62.   asm cli end;
  63.  
  64.   Port[ cStickPort] := $FF; { Write anything to trigger countdown }
  65.   repeat
  66.     b := Port[ cStickPort];
  67.     if b and 1 <> 0 then
  68.       Inc( gSticks[1].x);
  69.     if b and 2 <> 0 then
  70.       Inc( gSticks[1].y);
  71.     if b and 4 <> 0 then
  72.       Inc( gSticks[2].x);
  73.     if b and 8 <> 0 then
  74.       Inc( gSticks[2].y);
  75.     Inc( laps);
  76.   until (b and mask = 0) or (laps > 60000); { Timeout if no sticks connected }
  77.  
  78.   asm sti end;
  79.  
  80.   gSticks[1].present := b and 3 = 0;
  81.   gSticks[2].present := b and 12 = 0;
  82.  
  83.   if gSticks[1].present then
  84.   begin
  85.     gSticks[1].button1 := b and 16 = 0;
  86.     gSticks[1].button2 := b and 32 = 0;
  87.     gSticks[1].button3 := b and 64 = 0;
  88.     gSticks[1].button4 := b and 128 = 0;
  89.   end;
  90.  
  91.   if gSticks[2].present then
  92.   begin
  93.     gSticks[2].button1 := b and 64 = 0;
  94.     gSticks[2].button2 := b and 128 = 0;
  95.     gSticks[2].button3 := b and 16 = 0;
  96.     gSticks[2].button4 := b and 32 = 0;
  97.   end;
  98. end;
  99.  
  100.  
  101. { Initialization.
  102.   Assume both sticks present and do a poll.
  103.   The poll routine will clear present flags of non-connected sticks. }
  104.  
  105. procedure InitSticks;
  106. begin
  107.   gSticks[1].present := True;
  108.   gSticks[2].present := True;
  109.   PollSticks;
  110. end;
  111.  
  112.  
  113. end.
  114.